<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/6
 * Time: 上午11:49
 */
class PlayerService extends Service {

    public static function indexHeader(){
        $header = array(
        		"roleId"            => array('text' => '角色ID'),
        		"server"            => array('text' => '区服'),
                "account"           => array('text' => '账号'),
        		"roleName"          => array('text' => '角色名'),
                "vocation"          => array('text' => '职业'),
        		"channel"           => array('text' => '渠道'),
                "packet"            => array('text' => '包信息'),
                "fighting_capacity" => array('text' => '战力'),
                "grade"             => array('text' => '等级'),
                "surplus_treasure"  => array('text' => '元宝'),
                "last_login_time"   => array('text' => '最后登录时间'),
                "is_line"           => array('text' => '在线'),
        		'action'            => array('text' => '操作'),
        );
        return $header;

    }

    public static function indexData($params){
    	$result = array(
    			array('roleId'=>'101100010045','server'=>'S1','account'=>'sadad101100010045','roleName'=>'零晓亦','vocation'=>'法师','channel'=>'猫玩游戏','packet'=>'猫玩游戏32543252','fighting_capacity'=>'10000','grade'=>'100','surplus_treasure'=>1000,'last_login_time'=>'2018-07-02 20:18','is_line'=>'在线'),
    			array('roleId'=>'101100010046','server'=>'S1','account'=>'sadad101100010045','roleName'=>'零晓亦','vocation'=>'法师','channel'=>'猫玩游戏','packet'=>'猫玩游戏32543252','fighting_capacity'=>'10000','grade'=>'100','surplus_treasure'=>1000,'last_login_time'=>'2018-07-02 20:18','is_line'=>'在线'),
    			array('roleId'=>'101100010047','server'=>'S1','account'=>'sadad101100010045','roleName'=>'零晓亦','vocation'=>'法师','channel'=>'猫玩游戏','packet'=>'猫玩游戏32543252','fighting_capacity'=>'10000','grade'=>'100','surplus_treasure'=>1000,'last_login_time'=>'2018-07-02 20:18','is_line'=>'在线'),
    	);
    	
    	foreach($result as &$v){
    		$v['action'] = '';
    		$v['action']        .= '&nbsp;&nbsp;<a href="'.PubFunc::buildUrl('/oss/?r=player/view').'&roleId='.$v['roleId'].'" name="" data-id="'.$v['id'].'">查看</a>';
    	}
    	unset($v);
    	
    	return $result;
    }
    
    
    public static function typeTpl(){
    	$tpl = array(
    			'1'	 => 'basic_info',
    			'2'  => 'equipment',
    			'3'  => 'goods',
    			'4'  => 'skill',
    			'5'  => 'attribute',
    			'6'  => 'rune',
    			'7'  => 'soul',
    			'8'  => 'title',
    			'9'  => 'fashion',
    			'10' => 'sword',
    			'11' => 'metamorphosis',
    			'12' => 'faction',
    			'13' => 'achievement',
    			'14' => 'market',
    	);
    	return $tpl;
    }
    
    public static function playTab(){
    	$tab = array(
    			'1'	 => '基本信息',
    			'2'  => '身上装备',
    			'3'  => '背包物品',
    			'4'  => '玩家技能',
    			//'5'  => '玩家属性',
    			'6'  => '玩家符文',
    			'7'  => '玩家聚魂',
    			'8'  => '玩家称号',
    			'9'  => '玩家时装',
    			//'10' => '玩家剑魂',
    			'11' => '玩家幻形',
    			'12' => '玩家帮派',
    			'13' => '玩家成就',
    			'14' => '市场功能',
    	);
    	return $tab;
    }
    
    public static function isBlnd(){
    	$tab = array(
    			'1'	 => '绑定',
    			'0'  => '不绑定',
    	);
    	return $tab;
    }
    
    public static function slotList(){
    	$tab = array(
    			'1'	 => '武器',
    			'2'  => '头盔',
    			'3'  => '衣服',
    			'4'  => '裤子',
    			'5'  => '护手',
    			'6'  => '鞋子',
    			'7'  => '项链',
    			'8'  => '护符',
    			'9'  => '手镯',
    			'10' => '戒指',
    			'11' => '守护',
    			'12' => '同心领',
    	);
    	return $tab;
    }
    
    public static function goodsTypeList(){
    	$list = array(
    			//'All'           => '全部',
    			'itempack'      => '物品包',
    			'treasurepack'  => '寻宝背包',
    			'soulpack'      => '魂印包',
    			'soulequips'    => '聚魂装备包',
    			'warehouse'     => '仓库包',
    			'runeequips'    => '符文装备包',
    			'equips'        => '装备包',
    			'runepack'      => '符文包',
        );
    	return $list;
    }
    
    /*
     * 技能分类
     */
    public static function skillTypeList(){
    	$list = array(
    			'1'  => '普攻',
    			'2'  => '主动技能',
    			'3'  => '被动技能',
    			'4'  => '天书技能',
    			'5'  => '外观技能',
    			'6'  => '天赋技能',
    			'-1' => '无',
    	);
    	return $list;
    }
    
    public static function infoType_2($data,$good_type){
    	$goodsInfo = self::selectGoodsInfo($data);
    	
    	$isBlnd = self::isBlnd();
    	$slotList = self::slotList();
    	if(isset($good_type)){
    		 $goodsTypeList = self::goodsTypeList();
    	}
    	foreach ($data as &$v){
    		$v['stage'] = $goodsInfo[$v['class_id']]['stage'];
    		$v['star'] = $goodsInfo[$v['class_id']]['star'];
    		$v['quality'] = $goodsInfo[$v['class_id']]['quality'];
    		$color = self::mapColor($v['quality']);
    		$v['color'] = $color;
    		$v['name'] = $goodsInfo[$v['class_id']]['name'];
    		$v['bind'] = $isBlnd[$v['bind']];
    		$v['slot'] = $slotList[$v['slot']];
    		if(isset($good_type)){
    			$v['back'] = $goodsTypeList[$good_type];			
    		}
    	}
    	unset($v);
    	
    	return $data;
    }
    
    public static function infoType_4($data,$skill_type){
    	$skillInfo = self::selectSkillInfo($data);
    	$typeList = SkillService::typeList();
    	if($skill_type == -1){  //未指定分类
    		$skill_type_n = '0';
    	}else{
    		$skill_type_n = self::skillTypeList()[$skill_type];
    	}

    	$new_data = array();
    	foreach ($data as $key=>$v){
    		if($skillInfo[$v['class_id']]['backstage_type'] != $skill_type_n){
    			unset($v);
    		}else{
    			$v['name'] = $skillInfo[$v['class_id']]['name'];
    			$v['type'] = $typeList[$skillInfo[$v['class_id']]['type']];
    			$v['backstage_show'] = $skillInfo[$v['class_id']]['backstage_show']? '是':'否';
    			$v['backstage_type'] = $skillInfo[$v['class_id']]['backstage_type']? $skillInfo[$v['class_id']]['backstage_type'] : '无';
    			$v['backstage_level'] = $skillInfo[$v['class_id']]['backstage_level'];
    			$new_data[] = $v;
    		}
    	}

    	return $new_data;
    }
    
    public static function infoType_5($data){
    	ksort($data);
    	$result = array();
    	$attributeList = self::getAttributeList();
    	
    	$new_data = array();
    	foreach ($data as $k=>$v){
    		$d = array();
    		$d['a_id'] = $k;
    		$d['value'] = $v;
    		array_push($new_data,$d);
    	}
    	
    	$j = 0;
    	for($i=0;$i<count($new_data);$i++){
    		if($i%2 == 0){
    			$j++;
    		}
    		$result[$j][$i]['a_id'] = $attributeList[$new_data[$i]['a_id']]? $attributeList[$new_data[$i]['a_id']] : $new_data[$i]['a_id'];
    		$result[$j][$i]['value'] = $new_data[$i]['value'];
    	}
    	return $result;
    }
    
    public static function infoType_6($data){
    	$result = array();
        $g_id_array = array();
        $level_array = array();
        $new_data = array();
    	foreach ($data as $k=>$v){
    		$g_id_array[] = $v['class_id'];
    		$level_array[] = $v['level'];
    		$new_data[$v['class_id'].'-'.$v['level']] = $v;
    	}
    	
    	$modIntensifyAttr = new ModIntensifyAttr();
    	$result = $modIntensifyAttr->getInfo($g_id_array,$level_array);
    	
    	$attrList = IntensifyAttrService::attrList();
    	$game = PubBase::getCurrentGame();
    	$dictTmp = PubDict::dict($game, 'colortype');
    	foreach ($result as &$v){
    		$v['bind'] = $new_data[$v['g_id'].'-'.$v['level']]['bind'];
    		$v['slot'] = $new_data[$v['g_id'].'-'.$v['level']]['slot'];
    		$v['num'] = $new_data[$v['g_id'].'-'.$v['level']]['num'];
    		$color = self::mapColor($v['quality']);
    		$v['quality'] = $dictTmp[$v['quality']];
    		$v['color'] = $color;
    		$attr ='';
    		foreach ($attrList as $key=>$val){
    			if($v[$key] != 0){
    				$attr .= $val.'：'.$v[$key].'<br/>';
    			}
    		}
    		
    		if($attr == ''){
    			$v['attr'] = '无';
    		}else{
    			$v['attr'] = $attr;
    		}
    		
    	}
    	unset($v);
    	
    	return $result;
    }
    
    public static function infoType_8($data){
    	$title_array = array();
    	foreach ($data as $key=>$val){
    		$title_array[] = $val['class_id'];
    	}
    	
    	$modTitle = new ModTitle();
    	$params = array();
    	$game = PubBase::getCurrentGame();
    	$params['g'] = $game;
    	$result = $modTitle->select($params);
    	foreach ($result as &$v){
    		if(in_array($v['t_id'], $title_array)){
    			$v['have'] = '是';
    			$v['color'] = 'blue';
    		}else{
    			$v['have'] = '否'; //不拥有
    			$v['color'] = 'red';
    		}
    	}
    	unset($v);

    	return $result;
    }
    
    public static function infoType_9($data){
    	$a_fashion = array();
    	$a_level = array();
    	$fashionType = self::fashionType();
    	$types = '';
    	foreach ($fashionType as $key=>$val){
    		$types .= $key.',';
    		foreach ($data[$key]['appearances'] as $k=>$v){
    			$a_fashion[] = $k;
    			$a_level[$k] = $v['level'];
    		}
    	}
    	
    	$types = substr($types, 0, -1);
    	$modFashion = new ModFashion();
    	$fashionList = $modFashion->getInfoForType($types);
    	foreach ($fashionList as &$v){
    		if(in_array($v['f_id'], $a_fashion)){
    			$v['have'] = '是'; //拥有
    			$v['color'] = 'blue';
    			$v['level'] = $a_level[$v['f_id']];
    		}else{
    			$v['have'] = '否'; //不拥有
    			$v['color'] = 'red';
    			$v['level'] = '0';
    		}
    	}
    	unset($v);

    	return $fashionList;
    }
    
    public static function infoType_11($data){
    	$a_unreal = array();
    	$a_level = array();
    	$unrealType = self::unrealType();
    	$types = '';
    	foreach ($unrealType as $key=>$val){
    		$types .= $key.',';
    		foreach ($data[$key]['appearances'] as $k=>$v){
    			$a_unreal[] = $k;
    			$a_level[$k] = $v['level'];
    		}
    	}

    	$types = substr($types, 0, -1);
    	$modUnreal = new ModUnreal();
    	$list = $modUnreal->getInfoForType($types);
    	 
    	foreach ($list as &$v){
    		if(in_array($v['u_id'], $a_unreal)){
    			$v['have'] = '是'; //拥有
    			$v['color'] = 'blue';
    			$v['level'] = $a_level[$v['u_id']];
    		}else{
    			$v['have'] = '否'; //不拥有
    			$v['color'] = 'red';
    			$v['level'] = '0';
    		}
    	}

    	return $list;
    }
    
    public static function infoType_13($data){
    	$a_id_array = array();
    	$achievement_list = array();
    	foreach ($data as $key=>$val){
    		foreach ($val['ach_status'] as $k=>$v){
    			$a_id_array[] = $v['class_id'];
    			$achievement_list[$v['class_id']] = $v['status'];
    		}
    	}
    	
    	$modAchievement = new ModAchievement();
    	$result = $modAchievement->getInfo($a_id_array);
    	foreach ($result as &$v){
    		if($achievement_list[$v['a_id']] == 0){
    			$v['have'] = '否'; //不拥有
    			$v['color'] = 'red';
    		}else{
    			$v['have'] = '是'; //拥有
    			$v['color'] = 'blue';
    		}
    	}
    	unset($v);
    	return $result;
    }
    

    
    public static function fashionType(){
    	$list = array(
    			'10020000'      => '10020000',
    			'10070000'      => '10070000',
    	);
    	return $list;
    }
    
    public static function unrealType(){
    	$list = array(
    			'10010000'      => '10010000',
    			'10030000'      => '10030000',
    			'10050000'      => '10050000',
    			'10110000'      => '10110000',
    			'10060000'      => '10060000',
    			'10210000'      => '10210000',
    			'10140000'      => '10140000',
    	);
    	return $list;
    }

    public static function searchInfo($type,$data){
    	$result = array();
    	if($type == 1){
    		$result = $data;
    		if(count($data) > 0){
    			$result['last_login_time'] = isset($data['last_login_time'])? date('Y-m-d H:i:s',$data['last_login_time']) : '';
    			$result['last_logout_time'] = isset($data['last_logout_time'])? date('Y-m-d H:i:s',$data['last_logout_time']) : '';
    			$result['last_recharge_time'] = $data['last_recharge_time'] == 0? '无充值':date('Y-m-d H:i:s',$data['last_recharge_time']);
    			$result['total_online_time'] = number_format($data['total_online_time'] / 60, 2);
    			//$result['shutup_time'] = $data['shutup_time'] == 0? '0' : date('Y-m-d H:i:s',$data['shutup_time']);  //封禁时间
    			
    		}    		
    		//PubAuthSession::set('guild_uid', $data['guild_uid']);
    	}else if($type == 2){
    		$result = self::infoType_2($data);
    	}else if($type == 3){
    		$data = $data['player_itempack'];
    		$result = self::infoType_2($data,'itempack');
    	}else if($type == 4){   		
    		$result = self::infoType_4($data,'1'); //默认普攻
    	}else if($type == 5){ 
    		$result = self::infoType_5($data);	
    	}else if($type == 6){
    		$result = self::infoType_6($data);
    	}else if($type == 7){
    		$result = self::infoType_6($data);
    	}else if($type == 8){
    		$result = self::infoType_8($data);
    	}else if($type == 9){
    		$result = self::infoType_9($data);
    	}else if($type == 10){
    		$result = $data;
    	}else if($type == 11){ 
    		$result = self::infoType_11($data);
    	}else if($type == 12){
    		$result = $data;
    		if(count($data) > 0){
    			$result['create_time'] = isset($data['create_time'])? date('Y-m-d H:i:s',$data['create_time']) : '';
    		}		
    	}else if($type == 13){
    		$result = self::infoType_13($data);
    	}else if($type == 14){
    		$result = $data;
    	}
    	return $result;
    }
    
    public static function searchGoodsInfo($good_type,$data){
    	$result = array();
    	$data = $data['player_'.$good_type];
    	$result = self::infoType_2($data,$good_type);
    	return $result;
    }
    
    public static function searchSkillInfo($skill_type,$data){
    	$result = array();
    	$result = self::infoType_4($data,$skill_type);
    	return $result;
    }
    
    public static function getAttributeList(){
    	$modAttribute = new ModAttribute();
    	$game = PubBase::getCurrentGame();
    	$params = array();
    	$params['g'] = $game;
    	$attribute = $modAttribute->select($params);
    	$attributeList = array();
    	foreach ($attribute as $key=>$val){
    		$attributeList[$val['a_id']] = $val['name'];
    	}
    	return $attributeList;
    }
    
    public static function selectGoodsInfo($data){
    	$g_id_array = array();
    	foreach ($data as $key=>$val){
    		$g_id_array[] = $val['class_id'];
    	}
    	
    	$modGoods = new ModGood();
    	$goods_data= $modGoods->getInfo($g_id_array);
    	$new_goods_data = array();
    	foreach ($goods_data as $key=>$val){
    		$new_goods_data[$val['g_id']] = $val;
    	}
    	
    	return $new_goods_data;
    }
    
    public static function selectSkillInfo($data){
    	$g_id_array = array();
    	foreach ($data as $key=>$val){
    		$g_id_array[] = $val['class_id'];
    	}
    	
    	$modSkill = new ModSkill();
    	$skill_data= $modSkill->getInfo($g_id_array);
    	$new_skill_data = array();
    	foreach ($skill_data as $key=>$val){
    		$new_skill_data[$val['s_id']] = $val;
    	}
    	 
    	return $new_skill_data;
    }
    
    public static function autoComplete($val,$develop_id,$s_type){
    	$modRoleInfo = new ModRoleInfo();
    	if($s_type == 1){
    		$result = $modRoleInfo->getListByRoleName($val,$develop_id);
    		$complte = array();
    		 
    		foreach ($result as $k=>$v){
    			$complte[$k] = $v['rolename'];
    		}
    	} 

    	if($s_type == 2){
    		$result = $modRoleInfo->getListByRoleId($val,$develop_id);
    		$complte = array();
    		 
    		foreach ($result as $k=>$v){
    			$complte[$k] = $v['roleid'];
    		}
    	}

    	$complte_json = json_encode($complte);
    	return $complte;
    	 
    }
    
    public static function getRoleName($roleid){
    	$modRoleInfo = new ModRoleInfo();
    	$rolename = $modRoleInfo->getRoleName($roleid);
    	return $rolename;
    }
    
    public static function autoOpenId($val){
    	$modRoleInfo = new ModRoleInfo();
    	$result = $modRoleInfo->getListByOpenId($val);
    	$complte = array();
    	 
    	foreach ($result as $k=>$v){
    		$complte[$k] = $v['openid'];
    	}
    	$complte = array_unique($complte);
    	$complte_json = json_encode($complte);
    	return $complte;
    }
    
    public static function getRoleForOpenId($openid){
    	$modRoleInfo = new ModRoleInfo();
    	$result = $modRoleInfo->getRoleForOpenId($openid);
    	
    	$game = PubBase::getCurrentGame();
    	$serverList = PubServer::getOptions($platform, $game, 'develop_id');   	
    	
    	$list = array();
    	foreach ($result as $key=>$val){
    		$d = array();
    		$d['server'] = $serverList[$val['server']];
    		$d['level'] = $val['level'];
    		$d['roleid'] = $val['roleid'];
    		$d['rolename'] = $val['rolename'];
    		array_push($list, $d);
    	}   	
    	return $list;
    }
    
    /**
     * 账号封禁
     */
    public static function banUser($ar){
    	$params = array(
    			'bantype'       => $ar['ban_type'],
    			'list'          => $ar['accountid'],
    			'actiontype'    => $ar['status'],
    			'bandate'       => $ar['ban_date'],
    			'ban_forever'   => $ar['ban_forever'],
    			'reason'        => $ar['reason'],
    	);
    	$params['bandate'] = ($params['ban_forever'] == 1 || empty($params['bandate'])) ? 2000000000 : strtotime($params['bandate']);
    	$params['list'] = CommonService::getStringFromMultiLine($params['list']);
    	
    	unset($params['ban_forever']);
    	$api = new ApiService($ar['server_id']);
    	$result = $api->ban($params);
    	if($result['state']){
    		$out = array('status'=>1,'msg'=>'操作成功','data'=>'');
    	}else{
    		$out = array('status'=>0,'msg'=>$result['msg'],'data'=>'');
    	}
    	return $out;
    }
    
    /**
     * 角色禁言
     */
    public static function shutupUser($ar){
    	$params = array(
    			'actiontype'    => $ar['status'],
    			'channel'       => $ar['ban_channel'],
    			'list'          => $ar['rolename'],
    			'bandate'       => $ar['ban_date'],
    			'ban_forever'   => $ar['ban_forever'],
    			'reason'        => $ar['reason'],
    	);
    	$params['bandate'] = ($params['ban_forever'] == 1 || empty($params['bandate'])) ? 2000000000 : strtotime($params['bandate']);
    	$params['list'] = CommonService::getStringFromMultiLine($params['list']);
    	unset($params['ban_forever']);
    	
    	$api = new ApiService($ar['server_id']);
    	$result = $api->banChat($params);
    	if($result['state']){
    		$out = array('status'=>1,'msg'=>'操作成功','data'=>'');
    	}else{
    		$out = array('status'=>0,'msg'=>$result['msg'],'data'=>'');
    	}
    	return $out;
    }
    

}